package com.catherine.mvp_base_component.net;

import com.catherine.mvp_base_component.base.BaseRetrofitConfig;

import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @Description:retrofit管理
 * @Author: hhh
 * @CreateDate: 2020/9/15
 */
public class RetrofitManager {
    private volatile static RetrofitManager onlyApiRetrofit;
    private Retrofit mOnlyRetrofit;
    private volatile static RetrofitManager ethApiRetrofit;
    private Retrofit mEthRetrofit;
    private volatile static RetrofitManager phpServerApiRetrofit;
    private Retrofit mPhpServerRetrofit;


    /**
     * 获取only主网retrofit
     * @return RetrofitService
     */
    public static RetrofitManager getOnlyChainInstance() {
        if (onlyApiRetrofit == null) {
            synchronized (Object.class) {
                if (onlyApiRetrofit == null) {
                    onlyApiRetrofit = new RetrofitManager(ONLY);
                }
            }
        }
        return onlyApiRetrofit;
    }
 /**
     * 获取以太坊主网retrofit
     * @return RetrofitService
     */
    public static RetrofitManager getEthChainInstance() {
        if (ethApiRetrofit == null) {
            synchronized (Object.class) {
                if (ethApiRetrofit == null) {
                    ethApiRetrofit = new RetrofitManager(ETH);
                }
            }
        }
        return ethApiRetrofit;
    }
 /**
     * 获取后台retrofit
     * @return RetrofitService
     */
    public static RetrofitManager getPhpServerInstance() {
        if (phpServerApiRetrofit == null) {
            synchronized (Object.class) {
                if (phpServerApiRetrofit == null) {
                    phpServerApiRetrofit = new RetrofitManager(PHP);
                }
            }
        }
        return phpServerApiRetrofit;
    }


    private static final int ONLY = 1;
    private static final int ETH = 2;
    private static final int PHP = 3;
    /**
     * 初始化retrofit
     */
    private RetrofitManager(int type) {
        //配置okHttp并设置时间、日志信息和cookies
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        switch (type){
            case PHP:
                OkHttpClient php_okHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(httpLoggingInterceptor)
                        //设置超时时间
                        .connectTimeout(15, TimeUnit.SECONDS)
                        .build();

                //关联okHttp并加上rxJava和Gson的配置和baseUrl
                mPhpServerRetrofit = new Retrofit.Builder()
                        .baseUrl(BaseRetrofitConfig.getPhpBaseUrl())
                        .client(php_okHttpClient)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(BaseConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();
                break;
        }
    }

    /**
     * 利用泛型传入接口class返回接口实例
     *
     * @param ser 类
     * @param <T> 类的类型
     * @return Observable
     */
    public <T> T createOnlyRs(Class<T> ser) {
        return mOnlyRetrofit.create(ser);
    }

    public <T> T createEthRs(Class<T> ser) {
        return mEthRetrofit.create(ser);
    }
    public <T> T createPhpRs(Class<T> ser) {
        return mPhpServerRetrofit.create(ser);
    }
}
